from log.logger import logger
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


class Common():

    def __init__(self,driver=None):
        self.driver = driver

    def setDriver(self,driver):
        self.driver = driver

    def find_element_by_name(self,name):
        '''
        显示等待根据名字找元素
        :param name:
        :return:
        '''
        if name:
            element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.NAME, name)))
            return element
        else:
            logger.info('元素不能为空')

    def find_elements_by_name(self, name):
        '''
        显示等待根据名字找多个元素
        :param name:
        :return:
        '''
        if name:
            element = WebDriverWait(self.driver, 10).until(EC.presence_of_all_elements_located((By.NAME, name)))
            return element
        else:
            logger.info('元素不能为空')

    def find_elements_by_tag_name(self, name):
        '''
        显示等待根据名字找元素
        :param name:
        :return:
        '''
        if name:
            element = WebDriverWait(self.driver, 10).until(EC.presence_of_all_elements_located((By.TAG_NAME, name)))
            return element
        else:
            logger.info('元素不能为空')

    def switch_to_frames(self,*frames):
        '''
        切换多个frame
        :param driver:
        :param frame:
        :return:
        '''
        for frame in frames:
            self.driver.switch_to.frame(frame)

    def quit_and_switch_to_frames(self, *frames):
        '''
        退出切换多个frame
        :param driver:
        :param frame:
        :return:
        '''
        self.driver.switch_to.parent_frame()
        for frame in frames:
            self.driver.switch_to.frame(frame)
